﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Data
{
    public class SqlCrud
    {
        private static SqlCrud instance;

        public static SqlCrud getInstance
        {
            get
            {
                if (instance == null)
                {
                    instance = new SqlCrud();
                }
                return instance;
            }
        }

        public string insert(string tabela, List<string> campos, List<Object> values)
        {
            return "Insert into " + tabela + " (" + concatenarString(campos, false) + ") values (" + concatenarString(values, true) + ")";
        }

        public string selectAll(string tabela)
        {
            return "Select * From " + tabela;
        }

        public string select(List<Object> tabelas, List<string> campos, string condicao)
        {
            return "Select " + concatenarString(campos, false) + " From " + concatenarString(tabelas, false) + " Where " + condicao;
        }

        public string updateAll(string tabela, List<string> alteracoes)
        {
            return "Update " + tabela + " set " + concatenarString(alteracoes, false);
        }

        public string updateById(string tabela, List<string> alteracoes, string PK, int id)
        {
            return "Update " + tabela + " set " + concatenarString(alteracoes, false) + " Where " + PK + " = " + id;
        }

        public string deleteAll(string tabela)
        {
            return "Delete From " + tabela;
        }

        public string deleteById(string tabela, string PK, int id)
        {
            return "Delete From " + tabela + " Where " + PK + " = " + id;
        }

        private string concatenarString<T>(List<T> lista, bool aspas)
        {
            string str = "";

            foreach (var item in lista)
            {
                if (item.GetType() == typeof(string) && aspas) // completar com mas tipos que precisam de ''
                {
                    str += "'" + item + "'";
                }
                else
                {
                    str += item;
                }

                str += (lista.IndexOf(item) != lista.Count - 1) ? ", " : "";

            }
            return str;
        }


    }
}
